﻿using System.Runtime.InteropServices;
using System.ComponentModel;

namespace D.UtilSystemEnv
{
    #region 系统类型
    /// <summary>
    /// 系统类型
    /// </summary>
    public enum PlatformOS
    {
        /// <summary>
        /// linux os
        /// </summary>
        [Description("Linux")]
        Linux = 0,
        /// <summary>
        /// mac os
        /// </summary>
        [Description("OSX")]
        OSX = 1,
        /// <summary>
        /// windows os
        /// </summary>
        [Description("Windows")]
        Windows = 2
    }
    #endregion

    public class Platform
    {
        #region 获取平台操作系统
        /// <summary>
        /// 获取平台操作系统
        /// </summary>
        /// <returns></returns>
        public static PlatformOS GetOSPlatform()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return PlatformOS.Linux;
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return PlatformOS.OSX;
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return PlatformOS.Windows;
            return PlatformOS.Windows;
        }
        #endregion

        #region 服务器名称
        /// <summary>
        /// 服务器名称
        /// </summary>
        public static string MachineName { get; private set; } = Environment.MachineName;
        /// <summary>
        /// 系统名称
        /// </summary>
        public static string OSDescription { get; private set; } = RuntimeInformation.OSDescription;
        #endregion

        #region 系统及版本
        /// <summary>
        /// 系统及版本
        /// </summary>
        public static string OSVersion { get; private set; } = Environment.OSVersion.ToString();
        #endregion

        #region 系统框架
        /// <summary>
        /// 系统框架
        /// </summary>
        public static string FrameworkDescription { get; private set; } = RuntimeInformation.FrameworkDescription;
        #endregion

        #region 系统架构
        /// <summary>
        /// 系统架构
        /// </summary>
        public static Architecture OSArchitecture { get; private set; } = RuntimeInformation.OSArchitecture;
        /// <summary>
        /// 进程架构
        /// </summary>
        public static Architecture ProcessArchitecture { get; private set; } = RuntimeInformation.ProcessArchitecture;
        #endregion

        #region 当前项目目录

        /// <summary>
        /// 当前项目目录
        /// </summary>
        public static string CurrentDirectory { get; private set; } = Directory.GetCurrentDirectory();

        /// <summary>
        /// 系统目录
        /// </summary>
        public static string SystemDirectory { get; private set; } = Environment.SystemDirectory;
        #endregion

        #region 硬件检测
        /// <summary>
        /// 硬件监测
        /// </summary>
        /// <returns>硬件监测信息</returns>
        public static Object StartMonitoring()
        {

            int cpuCount = 0;// 获取CPU核心数
            float cpuLoad = 0;// 获取CPU占用率
            double cpuTemperature = 0;// 获取CPU温度
            List<DiskInfo> diskInfo = null;// 获取磁盘每个分区可用空间
            string localUsedIp = "";// 获取本机当前正在使用的IP地址
            RamInfo ramInfo = null;// 获取内存信息
            List<CpuInfo> CpuInfo = null;

            bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
            if (isLinux)
            {
                cpuCount = Environment.ProcessorCount;
                //TODO: Linux 硬件监测待完善
            }
            else
            {
                cpuCount = SystemInfo.GetCpuCount();// 获取CPU核心数
                cpuLoad = SystemInfo.CpuLoad;// 获取CPU占用率
                cpuTemperature = SystemInfo.GetCPUTemperature();// 获取CPU温度
                diskInfo = SystemInfo.GetDiskInfo();// 获取磁盘每个分区可用空间
                localUsedIp = SystemInfo.GetLocalUsedIP().ToString();// 获取本机当前正在使用的IP地址
                ramInfo = SystemInfo.GetRamInfo();// 获取内存信息
                CpuInfo = SystemInfo.GetCpuInfo();// 获取CPU信息
            }

            return new
            {
                CpuCount = cpuCount,
                CpuLoad = cpuLoad,
                CpuTemperature = cpuTemperature,
                CpuInfo = CpuInfo,
                LogicalDrives = Environment.GetLogicalDrives(),
                DiskInfo = diskInfo?.DistinctBy(t => t.SerialNumber),
                LocalUsedIP = localUsedIp,
                RamInfo = ramInfo,
                OSArchitecture = System.Enum.GetName(typeof(Architecture), RuntimeInformation.OSArchitecture),
                OSDescription = RuntimeInformation.OSDescription,
                ProcessArchitecture = System.Enum.GetName(typeof(Architecture), RuntimeInformation.ProcessArchitecture),
                FrameworkDescription = RuntimeInformation.FrameworkDescription,
                Windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows),
                Is64BitOperatingSystem = Environment.Is64BitOperatingSystem,
                Is64BitProcess = Environment.Is64BitProcess,
                OSVersion = Environment.OSVersion,
                CpuCore = Environment.ProcessorCount,
                HostName = Environment.MachineName
            };
        }
        #endregion
    }

   
}